//
// Created by bangsun on 2022/10/15.
//
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include "test_before.h"

void fun() {
    while (1) {
        printf("Hello, I'm fun!\n");
        sleep(1);
    }
}

void foo() {
    int stack[64] = {0};
    stack[63] = (int) fun;
    int new_esp = (int) (stack + 63);
    int prev_esp, prev_ebp;
    __asm__ volatile(
            "movl %%esp,%0\n"
            "movl %%ebp,%1\n"
            "movl %2,%%esp\n"

            :"=r"(prev_esp), "=r"(prev_ebp):"m"(new_esp):
            );
    printf("%x,%x,%x\n", *(stack - 1), *(stack - 2), *(stack - 3));
    printf("prev_esp:%x, prev_ebp:%x, cur_esp:%x, is_prev?%x\n", prev_esp, prev_ebp, new_esp, stack - 3);
    asm("ret");

}

int main() {
    foo();
    return 0;
}